<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">

    <div th:replace="~{commons/script::script}"></div>

</head>

<body>
<div>
    <span>联系人视图：</span>
    <span onclick="return whole('/contacts/toListContacts')"
          class="easyui-linkbutton" data-options="iconCls:'icon-ok',plain:true">全部</span>|
    &nbsp;&nbsp;&nbsp;
    <span onclick="return todayCreate('/contacts/toSelectNewCreateDate')" class="easyui-linkbutton"
          data-options="iconCls:'icon-ok',plain:true">今日添加</span>|
    <span onclick="return weekCreate('/contacts/toSelectNewCreateDate')" class="easyui-linkbutton"
          data-options="plain:true">本周添加</span>|
    <span onclick="return monthCreate('/contacts/toSelectNewCreateDate')" class="easyui-linkbutton"
          data-options="plain:true">本月添加</span>|
    <span onclick="return followed('/contacts/toSelectNewCreateDate')" class="easyui-linkbutton"
          data-options="plain:true">最近创建</span>|
    <span onclick="shared" class="easyui-linkbutton" data-options="plain:true">最近更新</span>|
    &nbsp;&nbsp;&nbsp;
    <span id="recycleBin" class="easyui-linkbutton" data-options="iconCls:'icon-recover_deleted',plain:true">回收站</span>
    &nbsp;
</div>

<div id="tools" style="margin-bottom: 20px">
    <table>
        <tr>
            <form action="/contacts/search">
                <td>
                    <i class="iconfont"></i>
                    <span onclick="return removes('/contacts/toDel')" class="easyui-linkbutton"
                          data-options="plain:true">批量删除</span>
                </td>
                <td>
                    <select name="filterSearch" style="width:100px;height:25px;">
                        <option value="-1">--任意字段--</option>
                        <option value="0">按联系人姓名查询</option>
                        <option value="1">按尊称查询</option>
                        <option value="2">按创建时间查询</option>
                        <option value="3">按电话查询</option>
                    </select>
                    <select name="contain" style="width:60px;height:25px;">
                        <option value="-1">--包含--</option>
                    </select>
                </td>
                <td class="text-content">
                    <input name="search" type="text" placeholder="请输入查询关键字">
                    <button type="submit" id="search" class="easyui-linkbutton"
                            data-options="iconCls:'icon-search',plain:true">搜索
                    </button>
                </td>
            </form>

            <td>
                <span id="chat" class="easyui-linkbutton" data-options="iconCls:'icon-cut',plain:true">发送短信</span>
                <span id="email" class="easyui-linkbutton" data-options="iconCls:'icon-redo',plain:true">发送邮件</span>
            </td>

            <td>
                <span onclick="return add('/contacts/cus')" class="easyui-linkbutton"
                      data-options="iconCls:'icon-add',plain:true">新建联系人</span>
            </td>
        </tr>
    </table>
</div>
<div id="customerLs" class="easyui-datagrid" style="width:100%;"></div>
</body>

<script type="text/javascript">
    $(function () {
        //分页展示客户列表
        $("#customerLs").datagrid({
            url: "/contacts/listContacts",
            checkOnSelect: false,
            idField: "customerId",
            pagination: true,
            sortName: "customerId",
            columns:[
                [
                    {field: "选择", checkbox: true},
                    {field: "id", hidden: true},

                    {field: "name", title: "联系人姓名",width:80},
                    {field: "saltname", title: "尊称",width:80},
                    {field: "customername", title: "所属客户",width:80},
                    {field: "telephone", title: "电话",width:80},
                    {field: "qqNo", title: "QQ",width:80},
                    {field: "email", title: "Email",width:80},
                    {field: "username", title: "创建人",width:80},
                    {
                        field: "createTime", title: "创建时间",width:80, formatter: function (value) {
                            return "<span title='" + value + "'>" + value + "</span>"
                        }
                    },
                    {
                        field: "operation", title: "操作", width: 80, formatter: function (value, rowData, index) {
                            return "<a href='javascript:void(0);' onclick=\"return see1(\'/contacts/sho\',\'" + rowData.id + "\')\">查看</a>" +
                                " <a href='javascript:void(0);' onclick=\"return modify1(\'/contacts/toUpdate\',\'" + rowData.id + "\')\">编辑</a>"
                        }
                    }

                ]
            ],
            loadFilter: function (data) {
                return {total: data.data.totalRows, rows: data.data.result}
            }

        })
    })

    //批量删除
    function removes(url) {
        var row = $("#customerLs").datagrid("getSelections");
        if (!row.length) {
            $.messager.alert("警告", "请选择要删除的数据");
            return;
        }

        $.messager.confirm("警告", "数据删除后无法恢复，是否确认删除", function (b) {
            if (b){
                for (let i of row) {
                    $.get(url, {id: i.id}, function (data) {
                        // $.messager.alert("警告",data.msg);
                        return;
                    })
                }
            }
            location.href = "/contacts/toListContacts";
        });
    }


    //查询全部客户
    function whole(url) {
        /*$("#customerLs").datagrid({
            "url": url
        })*/
        location.href = url;
    }


    //查询今天创建的客户
    function todayCreate(url) {
        /*$("#customerLs").datagrid({
            "url": url + "?type=day"
        })*/
        location.href = url + "?type=day";
    }

    //查询周创建的客户
    function weekCreate(url) {
        /*$("#customerLs").datagrid({
            "url": url + "?type=week"
        })*/
        location.href = url + "?type=week";
    }

    //查询月创建的客户
    function monthCreate(url) {
        /*$("#customerLs").datagrid({
             "url": url
         })*/
        location.href = url + "?type=month";
    }
    function followed(url) {
        location.href = url + "?type=week";
    }

    //页面添加客户
    function add(url) {
        parent.parent.openTopWindow({
            "url": url,
            "width": 800,
            "height": 700,
            "title": "新建联系人",
            close: function () {
                $("#customerLs").datagrid("reload");
            }
        })
    }

    //修改联系人
    function modify1(url, id) {
        parent.parent.openTopWindow({
            "url": url + "?id=" + id,
            "width": 800,
            "height": 700,
            "title": "修改联系人",
            close: function () {
                $("#customerLs").datagrid("reload");
            }
        })
    }

    //查看客户
    function see1(url, id) {
        parent.parent.openTopWindow({
            "url": url + "?id=" + id,
            "width": 800,
            "height": 700,
            "title": "客户信息",
            close: function () {
                $("#customerLs").datagrid("reload");
            }
        })
    }
</script>
<script type="text/javascript">
    $(function () {
        //邮件发送
        $("#email").on("click", function (e) {
            var rows = $("#customerLs").datagrid("getSelections");
            if (!rows || rows.length == 0) {
                $.messager.alert("警告", "请选中要发送邮件的客户");
                return;
            }
            if (rows.length > 1) {
                $.messager.alert("警告", "请选择单条数据进行发送邮件");
                return false;
            }
            var userId = rows[0].id;

            location.href = "/contacts/emails?id=" + userId;
        })

        //短信发送
        $("#chat").on("click", function (e) {
            var rows = $("#customerLs").datagrid("getSelections");
            if (!rows || rows.length == 0) {
                $.messager.alert("警告", "请选中要发送短信的客户");
                return;
            }
            if (rows.length > 1) {
                $.messager.alert("警告", "请选择单条数据进行短信发送");
                return false;
            }
            var userId = rows[0].id;

            location.href = "/contacts/chat?id=" + userId;
        })
    })
</script>
</html>
